﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wormhole.Engine.Communication
{
	/// <author>Hannes Foulds</author>
	/// <date>26 September 2008</date>
	/// <summary>
	/// A helper class to convert enums to and from string representations.
	/// </summary>
	public static class EnumConvertor<EnumType>
	{
		#region Convert to Int32
		/// <summary>
		/// Convert an action to its integer representation.
		/// </summary>
		/// <param name="action">The action to be converted.</param>
		/// <returns>The Int32 representation of the action.</returns>
		public static int ToInt32(EnumType action)
		{
			return Convert.ToInt32(action);
		}
		#endregion

		#region Convert From Int32
		/// <summary>
		/// Convert the integer representation of an action to its enumuration type.
		/// </summary>
		/// <param name="action">The action to convert.</param>
		/// <returns>Returns the integer representation.</returns>
		public static EnumType FromInt32(int action)
		{
			return EnumConvertor<EnumType>.FromString(action.ToString());
		}
		#endregion

		#region Convert To String
		/// <summary>
		/// Convert the action to its string representation.
		/// </summary>
		/// <param name="action">The action to be converted.</param>
		/// <returns>The string representation of the action.</returns>
		public static string ToString(EnumType action)
		{
			return EnumConvertor<EnumType>.ToInt32(action).ToString();
		}
		#endregion

		#region Convert From String
		/// <summary>
		/// Convert a string representation of an action to its enumuration type.
		/// </summary>
		/// <param name="action"></param>
		/// <returns></returns>
		public static EnumType FromString(string action)
		{
			return (EnumType)Enum.Parse(typeof(EnumType), action, true);
		}
		#endregion
	}
}
